$(function () {
    var paymentCheck_datagrid, paymentCheck_btn,payment_dialog,payment_form,search_form,paymentType_cb,orderBill_check_dialog,orderBill_check_form;
    paymentType_cb = $("#paymentType_cb");
    orderBill_check_dialog = $("#orderBill_check_dialog");
    orderBill_check_form = $("#orderBill_check_form");
    payment_dialog =$("#payment_dialog");
    search_form =$("#search_form");
    paymentCheck_datagrid = $("#paymentCheck_datagrid");
    paymentCheck_btn = $("#paymentCheck_btn");
    payment_form = $("#payment_form");
    //页面的展示
    paymentCheck_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/paymentCheck/list",
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        columns: [[
            {field: 'orderBill', title: '申请单号', width: 12, align: 'center', formatter: orderBillNumberFormatter},
            {field: 'insurancePolicyNumber', title: '保单号', width: 12, align: 'center', formatter: insurancePolicyNumberFormatter},
            {field: 'paymentDate', title: '缴费日期', width: 15, align: 'center', formatter: paymentDateFormatter},
            {field: 'client', title: '投保人', width: 10, align: 'center', formatter: clientFormatter},
            {field: 'paymentType', title: '缴费类型', width: 10, align: 'center', formatter: paymentTypeFormatter},
            {field: 'product', title: '产品', width: 13, align: 'center', formatter: productNameFormatter},
            {field: 'organization', title: '承保机构', width: 10, align: 'center', formatter: organizationFormatter},
            {field: 'beginTime', title: '起保日期', width: 10, align: 'center', formatter: beginTimeFormatter},
            {field: 'endTime', title: '停保日期', width: 10, align: 'center', formatter: endTimeFormatter},
            {field: 'issue', title: '期次', width: 8, align: 'center', formatter: issuetFormatter},
            {field: 'amount', title: '缴费金额', width: 10, align: 'center', formatter: amountFormatter},
            {field: 'coverage', title: '应缴金额', width: 10, align: 'center', formatter: coverageFormatter},
            {field: 'state', title: '保单状态', width: 10, align: 'center', formatter: stateFormatter},
            {field: 'serialNumber', title: '收据流水号', width: 15, align: 'center', formatter: serialNumberFormatter}
        ]]
    });
    //查看缴费信息窗口
    orderBill_check_dialog.dialog({
        title: '\&emsp;投保单信息',
        closable: true,
        minimizable: true,
        collapsible: true,
        width: 700,
        height: 400,
        closed: true,
        modal: true,
        resizable: true,
        buttons: "#orderBill_check_btn",
        onOpen:function () {
            $("#orderBill_check_form :input").prop("readonly",true);
        }
    });
    //
    //设置下拉列表框不能让用户自定义内容
    $(".easyui-combobox").combobox({"editable":false});
    // 统一管理点击事件
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    //统一处理点击事件
    var cmdObj = {
        //重置--清空高级查询数据
        reset:function(){
            search_form.form("reset");
            var everyNo  = $("[name=everyNo]").val();
            var issue  = $("[name=issue]").val();
            var amount  = $("[name=amount]").val();
            var clientName  = $("[name=clientName]").val();
            var paymentType  = $("[name=paymentType]").val();
            var productId  = $("[name=productId]").val();
            var state  = $("[name=state]").val();
            var organizationId  = $("[name=organizationId]").val();
            var minDate  = $("[name=minDate]").val();
            var maxDate  = $("[name=maxDate]").val();
            if (minDate == ""){
                minDate = '01/01/1949'
            }
            if (maxDate == ""){
                maxDate = '01/01/2088'
            }
            paymentCheck_datagrid.datagrid("load",{
                everyNo:everyNo,
                issue:issue,
                amount:amount,
                clientName:clientName,
                paymentType:paymentType,
                productId:productId,
                state:state,
                organizationId:organizationId,
                minDate:minDate,
                maxDate:maxDate
            })
        },
        //刷新--刷新整个页面
        reload:function () {
            alert("已刷新...");
        },
        //打印
        print:function () {
            var rowData = paymentCheck_datagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示","确定打印这条保单的数据吗?",function (yes) {
                    if (yes){
                        $.messager.alert("温馨提示","正在打印...请稍后...","info")
                    }
                })
            }else{
                $.messager.alert("温馨提示","请选一条数据进行打印!","info");
            }
        },
        //关键字搜索
        search:function () {
            var everyNo  = $("[name=everyNo]").val();
            var issue  = $("[name=issue]").val();
            var amount  = $("[name=amount]").val();
            var clientName  = $("[name=clientName]").val();
            var paymentType  = $("[name=paymentType]").val();
            var productId  = $("[name=productId]").val();
            var state  = $("[name=state]").val();
            var organizationId  = $("[name=organizationId]").val();
            var minDate  = $("[name=minDate]").val();
            var maxDate  = $("[name=maxDate]").val();
            if (minDate == ""){
                minDate = '01/01/1949'
            }
            if (maxDate == ""){
                maxDate = '01/01/2088'
            }
            paymentCheck_datagrid.datagrid("load",{
                everyNo:everyNo,
                issue:issue,
                amount:amount,
                clientName:clientName,
                paymentType:paymentType,
                productId:productId,
                state:state,
                organizationId:organizationId,
                minDate:minDate,
                maxDate:maxDate
            })
        },
        //
        //关闭投保单信息
        billBack:function () {
          orderBill_check_dialog.dialog("close");
        },
        //
        //查看投保单信息
        orderBill_check: function () {
            var rowData = paymentCheck_datagrid.datagrid("getSelected");
            //自定义orderBillId,便于paymentConfirm使用
            // $("#orderBillId").val(rowData.orderBill.id);
            if (rowData) {
                rowData['client.realName'] = rowData.client.realName;
                rowData['orderBill.orderBillNumber'] = rowData.orderBill.orderBillNumber;
                rowData['product.name'] = rowData.product.name;
                rowData['orderBill.beginTime'] = rowData.orderBill.beginTime;
                rowData['orderBill.endTime'] = rowData.orderBill.endTime;
                rowData['orderBill.issue'] = rowData.orderBill.issue;
                rowData['premium.amount'] = rowData.premium.amount;
                rowData['premium.coverage'] = rowData.premium.coverage;
                rowData['organization.organizationName'] = rowData.organization.organizationName;
                rowData['auditInsurance.pushTime'] = rowData.auditInsurance.pushTime;
                rowData['orderBill.state'] = rowData.orderBill.state;
                rowData['paymentConfirm.insurancePolicyNumber'] = rowData.paymentConfirm.insurancePolicyNumber;
                rowData['paymentHandle.serialNumber'] = rowData.paymentHandle.serialNumber;
                rowData['paymentConfirm.paymentDate'] = rowData.paymentConfirm.paymentDate;
                orderBill_check_dialog.dialog("open");
                $("#orderBill_check_form :input").prop("readOnly", true);
                orderBill_check_form.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择一条需要查看的数据", "info");
            }
        }
        //
    }
    //
});
function orderBillNumberFormatter(value, row, index) {
    return row.orderBill.orderBillNumber;
}

function insurancePolicyNumberFormatter(value, row, index) {
    return row.paymentConfirm.insurancePolicyNumber;
}

function paymentDateFormatter(value, row, index) {
    return row.paymentConfirm.paymentDate;
}

function serialNumberFormatter(value, row, index) {
    return row.paymentHandle.serialNumber;
}


function issuetFormatter(value, row, index) {
    return row.orderBill.issue;
}

function productNameFormatter(value, row, index) {
    return row.product.name;
}

function paymentTypeFormatter(value, row, index) {
    return row.orderBill.paymentType == 1 ? "<font>现金缴费</font>" : "<font>刷卡缴费</font>";
}

function beginTimeFormatter(value, row, index) {
    return row.orderBill.beginTime;
}

function endTimeFormatter(value, row, index) {
    return row.orderBill.endTime;
}

function stateFormatter(value, row, index) {
    if (row.orderBill.state == 31){
        return "<font>有效保单</font>";
    }else if (row.orderBill.state == 41){
        return "<font color='blue'>续保表单</font>";
    }else {
        return "<font color='red'>无效保单</font>";
    }
    //return row.orderBill.state == 31 ? "<font>有效保单</font>" : "<font color='red'>无效保单</font>";
}

function amountFormatter(value, row, index) {
    return row.premium.amount;
}

function coverageFormatter(value, row, index) {
    return row.premium.coverage;
}

function clientFormatter(value, row, index) {
    return row.client.realName;
}

function organizationFormatter(value, row, index) {
    return row.organization.organizationName;
}

